const app = getApp()
import {
    util,
    Toast,
    Dialog,
    commonMixin
} from "../../utils/util.js";
Page({
    behaviors: [commonMixin],
    /**
     * 页面的初始数据
     */
    data: {
        active:0,
        orderList:[],//订单
        page:1,
        serviceChat:false,
        showModel:false,
        actions: [],
        curSelcet:'',//当前选中的原因
        curtype:'', //退款还是退货
        curorder_no:'',
        payment_status:[
            '未支付', '支付失败', '进行中', '已退款', '已取消', '已删除', '已完成'
        ]
    },
    //复制订单号
    copy(e){
        let copytxt = e.currentTarget.dataset.order;
        wx.setClipboardData({
            data: copytxt,
            success (res) {

            }
        })
    },
  
    //改变
    onChange(event) {
        this.setData({
            active:event.detail.index,
            page:1,
            orderList:[],
        })
        this.getDataList()
    },
    onLoad(options) {
        if(options.index){
            this.setData({
                active:parseInt(options.index),
                page:1,
                orderList:[],
            })
        }
        // if(options.index && options.index==0){
            this.getDataList();
        // }
    },
    onShow() {
      let userInfo = util.getUserInfo()
      if (!userInfo.status) {
        app.globalData.loginFlag = true;
        this.navToUrl('/pages/my/index');
      }
    },
    onReachBottom() {
        this.getDataList();
    },
    onShareAppMessage() {

    },

    handleRefund(event) {
      let id = event.currentTarget.dataset.id;
      // wx.showLoading({
      //   title: '加载中',
      // })
      util.http.post("v1/mini/order-do-refund",{
        id: id
      }).then((res) => {
        if (res.data.code == 0) {
          let orderList = this.data.orderList;
          for (var i = 0; i < orderList.length; i++) {
            if (orderList[i].id == id) {
              orderList[i].payment_status = 3;
              break;
            }
          }
          this.setData({
            orderList:orderList
          });
          Toast('退款成功');
        } else {
          Toast(res.data.message);
        }
        // wx.hideLoading();
      }).catch((err) => {
          wx.hideLoading();
          Toast('网络异常！刷新后重试！');
          console.log(err)
      })
    },

    handlePay(event) {
        let id = event.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/shopCart/index?id=' + id
        })
    },

    handleRepay(event) {
      let checkitemid = event.currentTarget.dataset.checkitemid;
      wx.showLoading({
        title: '加载中',
      })
      util.http.post("v1/mini/generate-order",{
        check_item_id: checkitemid
      }).then((res) => {
          if (res.data.code == 0) {
            wx.navigateTo({
                url: '/pages/shopCart/index?id=' + res.data.data.id
            })
          } else {
            Toast(res.data.message);
          }
          wx.hideLoading();
      }).catch((err) => {
          wx.hideLoading();
          Toast('网络异常！刷新后重试！');
          console.log(err)
      })
    },

    handleLook(event) {
        let id = event.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/orderDetail/index?id=' + id
        })
    },
    //获取订单列表
    getDataList(isReset) {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        if (isReset) {
            this.setData({
                page:1,
                orderList:[],
            })
        }
        // let userInfo = util.getUserInfo();
		// let openid = userInfo.openid;
        util.http.post("v1/mini/get-order-list",{
            type:(this.data.active),
            page:this.data.page
        }).then((res)=>{
            let list = res.data.data.list;
            if(list.data && list.data.length>0){
                //合并数据
                let orderList = [...this.data.orderList,...list.data];
                this.setData({
                    orderList:orderList
                })
                //上拉刷新
                if(list.data && list.data.length>0){
                    let page = this.data.page;
                    this.setData({
                        page:page+1
                    })
                }
            }
            wx.hideLoading();
        }).catch((err)=>{
            wx.hideLoading();
            Toast('网络异常！刷新后重试！');
            console.log(err)
        })
    },
    handleDelete(event) {
        wx.showModal({
            title: '确定删除吗？',
            success: (res) => {
                if (res.confirm) {
                    let id = event.currentTarget.dataset.id;
                    wx.showLoading({
                        title: '加载中...',
                        mask: true
                    });
                    util.http.post("v1/mini/delete-order",{
                        id:id
                    }).then((res)=>{
                        if (res.data.code == 0) {
                          Toast('删除成功！');
                            this.getDataList(true)
                        } else {
                            Toast(res.data.message);
                        }
                        wx.hideLoading();
                    }).catch((err)=>{
                        wx.hideLoading();
                        Toast('网络异常！刷新后重试！');
                        console.log(err)
                    })
                }
            }
        })
    }
})